package com.example.newstest.until;

import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.databinding.InverseBindingMethod;
import android.databinding.InverseBindingMethods;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import com.example.newstest.R;

import com.squareup.picasso.Picasso;


@InverseBindingMethods({
        @InverseBindingMethod(type = ViewPager.class, attribute = "currentItem", method = "getCurrentItem")
})
public class Bindings {
    @BindingAdapter("loadUrl")
    public static void loadImage(ImageView view, String url) {
        Picasso.with(view.getContext())
                .load(url)
                .placeholder(R.mipmap.default_news)
                .into(view);
    }

    @BindingAdapter({"childCount", "childLayout"})
    public static void loadRadioButton(RadioGroup group, int count, int childLayoutId) {
        group.removeAllViews();
        LayoutInflater inflater = LayoutInflater.from(group.getContext());
        for (int i = 0; i < count; i++) {
            View view = inflater.inflate(childLayoutId, group, false);
            view.setId(i);
            group.addView(view);
        }
    }

    @BindingAdapter("currentItem")
    public static void setCurrentItem(ViewPager pager, int item) {
        pager.setCurrentItem(item);
    }

    @BindingAdapter("currentItemAttrChanged")
    public static void setCurrentItemChanged(ViewPager pager, final InverseBindingListener listener) {
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                listener.onChange();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @BindingAdapter("autoPlayDelayed")
    public static void setAutoPlayDelayed(final ViewPager pager, final int delayed) {
        Handler handler = (Handler) pager.getTag();
        if (handler == null) {
            handler = new Handler(Looper.getMainLooper()) {
                @Override
                public void handleMessage(Message msg) {
                    switch (msg.what) {
                        case 0:
                            int index = pager.getCurrentItem() + 1;
                            if (index >= pager.getAdapter().getCount()) {
                                index = 0;
                            }
                            pager.setCurrentItem(index);
                            sendEmptyMessageDelayed(0, delayed);
                            break;
                    }
                }
            };
            pager.setTag(handler);
        }
        handler.removeMessages(0);
        if (delayed > 0) {
            handler.sendEmptyMessageDelayed(0, delayed);
        }
    }

    @InverseBindingAdapter(attribute = "topPosition")
    public static int getRecyclerTopPosition(RecyclerView view) {
        return view.getChildAdapterPosition(view.getChildAt(0));
    }

    @BindingAdapter("topPositionAttrChanged")
    public static void setListener(RecyclerView view, final InverseBindingListener listener) {
        view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                listener.onChange();
            }
        });
    }

    @BindingAdapter("topPosition")
    public static void setTopPosition(RecyclerView view, int topPosition) {
        view.scrollToPosition(topPosition);
    }
}
